package edu.fsu.cs.SHPE;

import java.net.ConnectException;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SponsorListAdapter extends BaseAdapter {

	private final static String TAG = "SHPE.SponsorListAdapter";
	private SponsorContainer sponsorContainer;
	private LayoutInflater inFlate;
	
	
	public SponsorListAdapter(Context context, String url) throws ConnectException {
		sponsorContainer = new SponsorContainer(url);
		inFlate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
	}
	
	public void setViewableSponsorsByLevelId(long id){
		sponsorContainer.setViewableSponsorsByLevelId(id);
	}
	
	
	@Override
	public int getCount() {
		return sponsorContainer.getViewableList().size();
	}

	@Override
	public Object getItem(int position) {
		return sponsorContainer.getViewableList().get(position);
	}

	@Override
	public long getItemId(int position) {
		return sponsorContainer.getViewableList().get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.i(TAG, "getting view " + position);
		
		if(convertView == null)
			convertView = inFlate.inflate(R.layout.sponsoritem, null);
		
		TextView tmp;
		Sponsor tmpSponsor = sponsorContainer.getViewableList().get(position);
		
		
		if(tmpSponsor.getLogo() != null){
			ImageView logo = (ImageView)convertView.findViewById(R.id.imgLogo);
			logo.setImageDrawable(tmpSponsor.getLogo());
		}
		
		if(tmpSponsor.getName() != null){
			tmp = (TextView)convertView.findViewById(R.id.txtName);
			tmp.setText(tmpSponsor.getName());
		}
		
		return convertView;
	}

}
